package mao.serializable;

import java.io.*;

/**
 * @author Totoro
 * @date 2020/7/13
 * @explain:
 */
public class Person2 implements Serializable {
    private transient  String name ;
    private transient  int age; //不需要序列化的字段。
    private int height;

    public Person2(String name, int age,int height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }

    @Override
    public String toString() {
        return "Person2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", height=" + height +
                '}';
    }

    public static void main(String[] args) throws Exception{
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.txt"));
        Person2 mao = new Person2("mao", 25,173);
        oos.writeObject(mao);


        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.txt"));
        Person2 o1 = (Person2)ois.readObject();
        System.out.println(o1);
    }

    //自定义写对象和读对象。
    private void writeObject(ObjectOutputStream out) throws IOException {
        out.writeObject(new StringBuilder(this.name).reverse());
        out.writeObject(this.age);
    }

    private void readObject(ObjectInputStream ins) throws IOException,ClassNotFoundException{
        this.name = ((StringBuilder) ins.readObject()).reverse().toString();
        this.age = (int) ins.readObject();
    }


}
